package com.chaosj.webmagic.process;

import cn.hutool.core.text.StrFormatter;
import com.alibaba.fastjson.JSONObject;
import com.chaosj.webmagic.md.entity.BlogArticle;
import org.apache.http.impl.client.CloseableHttpClient;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @className: BasePageProcessor
 * @description:
 * @author: caoyangjie
 * @date: 2022/1/5
 **/
public abstract class BasePageProcessor implements PageProcessor {
    public static final String CUR_DEPTH = "curDepth";
    protected AtomicInteger maxDepth = new AtomicInteger(1);
    private JSONObject params;

    @Override
    public void process(Page page) {
        String url = page.getUrl().toString();
        Boolean error = false;
        try {
            String title = page.getHtml().xpath(titleXPath()).get().trim();
            System.out.println(StrFormatter.format("成功抓取到文章: {} url: {}", title, url));
            String author = page.getHtml().xpath(authorXPath()).get();
            String content = page.getHtml().xpath(contentXPath()).get();
            BlogArticle blog = new BlogArticle().setAuthor(author).setContent(content).setTitle(title).setUrl(url).setPublishDate(LocalDate.now().toString());
            page.putField("blog", blog);
        } catch (Exception e){
            error = true;
        } finally {
            extractNextPage(page, error);
            doMoreProcess(page, error);
        }
    }

    /**
     * 获取 url 到xpath路径
     * @return
     */
    protected abstract String titleXPath();

    /**
     * 获取 author 到xpath路径
     * @return
     */
    protected abstract String authorXPath();

    /**
     * 获取 content 到xpath路径
     * @return
     */
    protected abstract String contentXPath();

    protected String errorContentXpath(){
        return "";
    }

    protected void extractNextPage(Page page, Boolean error){
        Object curDepth = page.getRequest().getExtra(CUR_DEPTH);
        if( curDepth!=null && Integer.valueOf(curDepth.toString()) > this.getParams().getIntValue("maxDepth")){
            return;
        }
        maxDepth.incrementAndGet();
        List<Request> nextPages = page.getHtml().xpath(error?errorContentXpath():contentXPath()).xpath("//a").nodes().stream().filter(selectable -> filter(selectable)).map(href->{
            Request request = new Request(href.links().get());
            request.setExtras(page.getRequest().getExtras()).putExtra(CUR_DEPTH, this.maxDepth.get());
            return request;
        }).collect(Collectors.toList());
        page.getTargetRequests().addAll(nextPages);
    }

    /**
     * 执行登录逻辑
     * @param loginClient
     */
    public void doLogin(CloseableHttpClient loginClient){

    }

    public void setParams(JSONObject params){
        this.params = params;
        int maxDepth = params.getIntValue("maxDepth");
    }

    public JSONObject getParams() {
        return params;
    }

    protected boolean filter(Selectable selectable){
        if( selectable.links().get().startsWith("http") ){
            return true;
        }
        return false;
    }

    protected void doMoreProcess(Page page, Boolean error){

    }
}
